/*
 * Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef MYAPPLICATION_AUDIOPLAYER_H
#define MYAPPLICATION_AUDIOPLAYER_H
#include <string>
#include <multimedia/player_framework/avplayer.h> 

class AudioPlayer {
    std::string m_strId;
    void* m_pHandler;
public:
    static const int const_media_state = 1;
    static const int const_media_duration = 2;
    static const int const_media_position = 3;
    static const int const_media_error = 9;
    
    static const int const_media_err_none_active = 0;
    static const int const_media_err_aborted = 1;
    enum  State {
        IDLE,       // 空闲
        INITIALIZED,//初始状态
        PREPARED,   // 准备完成
        PLAYING,    // 正在播放
        PAUSED,     // 已暂停
        STOPPED,    // 已停止
        COMPLETED,  //结束
        RELEASED,   //释放
        ERROR       // 出错
    };
    
    enum MediaState {
        MEDIA_NONE,
        MEDIA_STARTING,
        MEDIA_RUNNING,
        MEDIA_PAUSED,
        MEDIA_STOPPED,
        MEDIA_LOADING
    };
    
    AudioPlayer(void * pHandler, const std::string& strId);
    ~AudioPlayer();

    bool create();
    bool release();
    bool setSource(const std::string &url);
    bool prepare();
    bool play();
    bool pause();
    bool stop();
    bool reset();
    bool seekTo(long mSec);
    bool setVolume(float fVolume);
    bool setPlaybackSpeed(float fSpeed); 
    float getCurrentSpeed() const;
    bool getCurrentPosition(int& nPosition);
    bool getDuration(int& nPosition) const;
    State getState() const;
    void setState(AudioPlayer::State state);
    void* getHandler();
    void sendErrorStatus(int nErrorCode, std::string strErrorMessage);
    void sendStatusChange(int nMessageType, int nAdditionalCode, float fValue, std::string strErrorMessage);
private:
    OH_AVPlayer* m_player;
    State m_currentState;
    float m_fCurrentSpeed;
    static void PlayerOnInfoCallback(OH_AVPlayer *player, AVPlayerOnInfoType type, OH_AVFormat* infoBody,void *userData);
    static void PlayerOnErrorCallback(OH_AVPlayer *player, int32_t errorCode, const char *errorMsg,void *userData);
};
#endif //MYAPPLICATION_AUDIOPLAYER_H
